package be.djdb.preparts;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamSource;


import org.apache.fop.apps.FOPException;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FopFactory;

import be.djdb.Statics;
import be.djdb.utils.TransformtypeEnum;
/**
* @author Lieven Roegiers
* @copyright 2007
* @from JAVA_mylibs
* @version java 1.5+
*/
public class XMLtransformer implements Serializable{
	private static final long serialVersionUID = -7441985891157554514L;
	private Transformer transformer;
	private SAXResult saxresult;
		 /**<%@ page contentType="text/html; charset=" %>
		 * use fop.jar   => find on http://www.java2s.com/Code/Jar/fop/fop.jar.htm
		 * or use fop.jar  => http://archive.apache.org/dist/xmlgraphics/fop/binaries/fop-1.0-bin.zip
		 * @param xml source => accept: StreamSource
		 * @param xsl source
		 * @param Outputstream returns pdf or doc or ect
		 * @param transformtype  TAG(String,String);
		 * @throws TransformerException 
		 * @throws FOPException 
		 * @error The type org.apache.fop.apps.FOPException cannot be resolved. It is indirectly referenced from required .class files
		 * LIKE not included foplib 
		 * and the hint like "Configer build path" is missig fop.jar
		 * <pre>
		  	     File xmlin = new File("testen/test/Testfile.xml"); 
		  		 File xslin = new File("testen/Testfile.xsl");
		  		 XMLtransformer xmltransform = new XMLtransformer();
		  				if(xmlin.exists()&& xslin.exists()){
		  				    InputStream xmlStream = new FileInputStream(xmlin);
		  					InputStream xslStream = new FileInputStream(xslin);
		  					OutputStream outstream =new FileOutputStream(new File("testen/bank.pdf"));
		  					xmltransform.generate(xmlStream,xslStream,outstream,TransformtypeEnum.PDF);
		  				}
		  </pre>
		 * <pre>
		  	     fileUrl = this.getClass().getClassLoader().getResource("Testfile.xsl");
		  		 InputStream xsl = fileUrl.openConnection().getInputStream();
		  		 Document xmldoc = xmlgenerate(stat);
		  		 DOMSource source = new DOMSource(xmldoc);
		  		 ByteArrayOutputStream out = new ByteArrayOutputStream();
		  		 new XMLtransformer().generate(source,xsl,out,TransformtypeEnum.PDF);
		  		 byte[] abytearray = out.toByteArray(); 
		  </pre>
		 */
		public void generate(InputStream xml,InputStream xslin ,ByteArrayOutputStream byteoutst,TransformtypeEnum transformtype) throws Exception{
			make(new StreamSource(xml),new StreamSource(xslin),byteoutst,transformtype);
		}
		public void generate(Source xml,InputStream xslin ,ByteArrayOutputStream byteoutst,TransformtypeEnum transformtype) throws Exception{
			make(xml,new StreamSource(xslin),byteoutst,transformtype);
		}
		public XMLtransformer() {
			log(Level.WARNING,"@XMLtransformer");
		}
		public void generate(InputStream xml,InputStream xslin ,OutputStream outst,TransformtypeEnum transformtype) throws Exception{

			make(new StreamSource(xml),new StreamSource(xslin),outst,transformtype);
		}
		public void generate(Source xml, Source xsl,OutputStream outst, TransformtypeEnum transformtype) throws FOPException, TransformerException {
					make(xml,xsl,outst,transformtype);
		}
		public void generate(Source xml, InputStream xslin,OutputStream outst, TransformtypeEnum transformtype) throws FOPException, TransformerException, IOException {
			make(xml,new StreamSource(xslin),outst,transformtype);
		}
		 /**<%@ page contentType="text/html; charset=" %>
		 * use fop.jar find on http://www.java2s.com/Code/Jar/fop/fop.jar.htm
		 * @param xml source 
		 * @param xsl source
		 * @param Outputstream returns pdf or doc or ect
		 * @param transformtype  TAG(String,String);
		 * @throws TransformerException 
		 * @throws FOPException 
		 */
		private void make(Source xml,Source xsl ,OutputStream ost,TransformtypeEnum transformtype) throws TransformerException, FOPException{
			try{	
				TransformerFactory transformerfactory = TransformerFactory.newInstance();
			    transformer = transformerfactory.newTransformer(xsl);
			    FopFactory fopfactory = FopFactory.newInstance();
			    Fop fop = fopfactory.newFop(transformtype.getFopoutputvalue(), ost);
			    saxresult = new SAXResult(fop.getDefaultHandler());
			    transformer.transform(xml,saxresult);
			}catch (TransformerConfigurationException e) {
				System.out.println("fout in xsl tenopzichte van xml");
				e.printStackTrace();
			}finally{
			}
		}
	    private static  void log(Level level, String msg ){
	        String tag ="<>>>>*"+Statics.LIBNAME+"-"+Statics.COPYRIGHTBY+"*<<<<>StreamManipulator";
	        Logger.getLogger(XMLtransformer.class.getName()).log(level, tag + msg);
	    }
}
